/*
 * Project name:
     EasyPIC Slalom
 * Author:
     Samuel.B.Atlan@gmail.com
 * Revision History:
     2010/04/14: initial release;
 * Description:
     Turns on/off LEDs with a slalom effect.
     Pressing RE0 will change the spiral direction.
 * Test configuration:
     MCU:             PIC16F887
     Dev.Board:       EasyPIC6
     Oscillator:      MCU 4.00 MHz, Internal RC no Clock (RA6 & RA7 as I/O)
     Ext. Modules:    -
     Software:        mikroC PRO for PIC 3.2
 * Notes:
     - TO DO: use interrupt to change slalom direction.
*/

const unsigned long waiting = 50;
char counter;
char i;
bit change_direction;


void left2right(char x) {
  PORTA |= 1 << x;
  Delay_ms(waiting);
  PORTB |= 1 << x;
  Delay_ms(waiting);
  PORTC |= 1 << x;
  Delay_ms(waiting);
  PORTD |= 1 << x;
  Delay_ms(waiting);
}

void right2left(char x) {
  PORTD |= 1 << x;
  Delay_ms(waiting);
  PORTC |= 1 << x;
  Delay_ms(waiting);
  PORTB |= 1 << x;
  Delay_ms(waiting);
  PORTA |= 1 << x;
  Delay_ms(waiting);
}

void main() {
  ANSEL  = 0;                                    // Configure AN pins as digital
  ANSELH = 0;
  C1ON_bit = 0;                                  // Disable comparators
  C2ON_bit = 0;

  TRISA = 0x00;                                  // set direction to be output
  TRISB = 0x00;                                  // set direction to be output
  TRISC = 0x00;                                  // set direction to be output
  TRISD = 0x00;                                  // set direction to be output
  TRISE = 0x01;                                  // RE0 as input

  change_direction = 0;
  
  while (1) {
// Clear PORTs
    PORTA = 0x00;
    PORTB = 0x00;
    PORTC = 0x00;
    PORTD = 0x00;
    PORTE = 0x00;

// Make Spiral
    i = 0;
    if (PORTE) {
       change_direction = 1 - change_direction;
       }
    if (change_direction) {
      for (i ; i<8 ; i++, i++) {
        left2right(i);
        right2left(i+1);
      }
    }
    else {
      for (i=8 ; i>1 ; i--, i--) {
        left2right(i-1);
        right2left(i-2);
      }
    }
  }
}
